<?php

class Controller_search extends FLEA_Controller_Action
{
    function actionindex(){
        $word = trim($_POST['word'])?trim($_POST['word']):trim($_GET['word']);
        if($word){
            $smarty =& $this->_getView();
            $smarty->assign('word', $word);
            $smarty->display('search.html');
        }
        else{
            redirect('index.php');
        }
    }
    function actionimage(){
        $word = $_GET['word']?trim($_GET['word']):"hello";
        FLEA::loadFile('FLEA_FLEA_Ajax_JSON.php',true);
        $json = new Services_JSON();
        $table_pics =& FLEA::getSingleton('Table_tofowordpic');
        // $pics = $table_pics->getpicbyword($word);
        // $pics = $table_pics->findBySql('SELECT a.id as pic_id,c.id as voice_id,b.pic_text,b.pic_text_zh FROM `tofoword_pic` a inner join (tofoword_text b,tofoword_voice c) on (a.No=b.No and a.Word=b.Word and a.No=c.No and a.Word=c.Word) where a.Word ="' . $word . '" and a.status=1 order by a.rank');
        $pics = $table_pics->findBySql('SELECT a.id as pic_id,b.pic_text,b.pic_text_zh,a.Word,a.No FROM `tofoword_pic` a inner join (tofoword_text b) on (a.No=b.No and a.Word=b.Word) where a.Word ="' . $word . '" and a.status=1 order by a.rank');
        print $json->encode($pics);
    }
    function actionmean(){
        $word = $_GET['word']?trim($_GET['word']):"hello";
        FLEA::loadFile('FLEA_FLEA_Ajax_JSON.php',true);
        $json = new Services_JSON();
        $table_yinbiao = & FLEA::getSingleton('Table_word2yinbiao');
        $yinbiao = $table_yinbiao->find(array('Word'=>$word),null,'Word,YinBiao,HanYuJieshe');
        $yinbiao[YinBiao] = iconv("GB2312","UTF-8",$yinbiao[YinBiao]);
        $yinbiao[HanYuJieshe] = iconv("GB2312","UTF-8",$yinbiao[HanYuJieshe]);
        print $json->encode($yinbiao);
    }
    function actionsentence(){
        $word = $_GET['word']?trim($_GET['word']):"hello";
        FLEA::loadFile('FLEA_FLEA_Ajax_JSON.php',true);
        $json = new Services_JSON();
        $table_sentence = & FLEA::getSingleton('Table_wordsentence');
        $sentence = $table_sentence->findAll(array('Word'=>$word,'status'=>1),'rank',null,'id,Word,En_Sen,No,Chi_Sen');
        foreach($sentence as &$value) {
            $value['Chi_Sen'] = iconv("GB18030","UTF-8",$value['Chi_Sen']);
        }
        print $json->encode($sentence);
    }
    function actionshow(){
        $table_words = & FLEA::getSingleton('Table_tofowordpic');
        $smarty =& $this->_getView();
        $words = $table_words->find((int)$_GET['id']);
        $image =  imagecreatefromstring($words[PicBuf]);
        Header("Content-type:image/png"); 
        imagepng($image);
    }
    function actionshowbysize(){
        $table_words = & FLEA::getSingleton('Table_tofowordpic');
        $smarty =& $this->_getView();
        $words = $table_words->find((int)$_GET['id']);
        $image = imagecreatefromstring($words[PicBuf]);
        FLEA::loadFile('APP_Helper_MyImage');
        $out = new resizeimage($image,400,300,0);
        $image = $out->resize();
        Header("Content-type:image/png"); 
        imagepng($image);
    }
    function actiongeneratedata(){
        $categories = FLEA::loadFile('Config_WordCategory.php');
        // dump($categories);
        $words = array();
        foreach($categories as $value){
            foreach($value as $v){
                $table_words = & FLEA::getSingleton('Table_'.$v);
                $word = $table_words->findAll();
                foreach($word as $vv){
                    $words[] = $vv[Word];
                }
            }
        }
        sort($words);
        $words = array_unique($words);
        print "生成单词数:" . count($words);
        $js = 'var words = ["' . join('","', $words) . '"];';
        safe_file_put_contents("_Shared/_Cache/words.js",$js);
    }
}
